package com.example.p2pmessages;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Messagehandler {

	public String messageString;
	private int newSenderPort = 49081;
	private int listenPort = 50005;
	private Date sendtime;
	private Date receivetime;
	private OnMessage mStore = new MessageStore();
	private String sentMessage;

	public Messagehandler() {
	}

	public void sendMessage(String message, String senderip)
			throws java.io.IOException {
		this.sentMessage = message;
		Date date = new Date();
		DatagramSocket socket = new DatagramSocket();
		InetAddress serverIP = InetAddress.getByName(senderip);
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss:S");
		String timestr = sdf.format(date);
		String modMessage = message + ";" + timestr;
		this.sentMessage = modMessage;
		byte[] outmessage = (modMessage).getBytes();
		DatagramPacket out = new DatagramPacket(outmessage, outmessage.length,
				serverIP, newSenderPort);
		socket.send(out);
		socket.close();
	}

	public void getMessage() throws ClassNotFoundException, ParseException {
		byte[] inmessage = new byte[48];
		byte[] outmessage = new byte[48];
		String message;
		DatagramSocket socket;
		try {
			socket = new DatagramSocket(listenPort);
			while (true) {
				DatagramPacket in = new DatagramPacket(inmessage,
						inmessage.length);
				socket.receive(in);
				InetAddress senderIP = in.getAddress();
				int senderPort = in.getPort();
				System.out.println(in.getSocketAddress());

				message = new String(in.getData(), 0, in.getLength());
				String[] modMessage = message.split(";");
				System.out.println("Message " + modMessage[0] + " from "
						+ senderIP + ", " + senderPort);
				messageString = "Message " + message + " from " + senderIP
						+ ", " + senderPort;

				SimpleDateFormat s = new SimpleDateFormat("hh:mm:ss:SSS");
				Date sendDate = s.parse(modMessage[1]);
				String timestamp = s.format(new Date());
				Date date = new Date();
				String timestr = s.format(date);
				Date currDate = s.parse(timestr);
				this.receivetime = currDate;
				this.sendtime = sendDate;
				long diff = this.receivetime.getTime()
						- this.sendtime.getTime();
				long rtt;
				
				if (diff<0)
					rtt = diff*(-1) % 100;
				else
					rtt = diff % 100;
		

				Message m = new Message(date, messageString, rtt, senderIP,
						senderPort, this.sentMessage, timestamp);
				mStore.add(m);
				/*
				 * outmessage = "Pong".getBytes(); DatagramPacket out = new
				 * DatagramPacket(outmessage, outmessage.length, senderIP,
				 * senderPort); socket.send(out);
				 */
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
